package model;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.List;
import java.util.Scanner;


/**
 * Abstract class for retrieving data from different types of data files.
 * @author Niel Lebeck
 *     edited by Jenni Mercado
 */
public abstract class DataReader {

    /**
     * Reads in a data file and returns a list of WebsiteVisit objects representing
     * the website visit data in the file.
     * @param dataFile file containing data
     * @return List containing all WebsiteVisits in the data
     * @throws WebHistoryDataException for input errors
     */
    public abstract List<WebsiteVisit> readData (File dataFile)
        throws WebHistoryDataException;

    /**
     *
     * @param dataFile data file
     * @return Scanner for the data file
     * @throws WebHistoryDataException if data file is not supported
     */
    protected Scanner getScanner(File dataFile) throws WebHistoryDataException {
        if (!dataFile.getName().endsWith(getEnding())) {
            throw new WebHistoryDataException("data file not supported");
        }
        Scanner input = null;
        try {
            input = new Scanner(dataFile);
        }
        catch (FileNotFoundException e) {
            throw new WebHistoryDataException("file not found");
        }
        return input;
    }

    /**
     * Template method.
     * @return the file ending supported by this DataReader subclass
     */
    public abstract String getEnding();

}
